/**
 * 
 */
package com.android.hw.projectmenu;



import com.qualcomm.qcnvitems.QcNvItems;
import com.android.hw.projectmenu.R;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.text.TextUtils;
import android.util.Log;

/**
 * @author gaoshengpan
 *
 */
public class ManufactureInfo extends PreferenceActivity {
    private static final String MACHINE_CHARGE = "machine_charge";
    private static final String COUPLING_TEST = "coupling_test";
    private static final String MMI1_TEST = "mmi1_test";
    private static final String MACHINE_CHECK = "machine_check";
    private static final String FACTORY_DATA_RESET = "factorydate_reset";
    private static final String SMTSN_NUM = "smtsn_num";
    private static final String FTBT_MARK = "ftbt_mark";
    private static final String MMI2_TEST = "mmi2_test";
    private static final String AGEING_TEST = "ageing_test";
    private static final String AUDIO_TEST = "audio_test";
    private static final String GSM_CALIBRATION = "gsm_calibration";
    private static final String GSM_TEST = "gsm_test";
    private static final String CDMA_CALIBRATION = "cdma_calibration";
    private static final String CDMA_TEST = "cdma_test";
    private static final String LTE_CALIBRATION = "lte_calibration";
    private static final String LTE_TEST = "lte_test";
    
    private String TAG = "ManufactureInfo";

    private static String mUnknown = null;

    private QcNvItems mNv;

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            switch (msg.what) {

            case 0:

                break;
            case 1:
                getNvData();
                break;
            default:
                break;
            }
        }
    };

    /**
     * 
     */
    public ManufactureInfo() {
        // TODO Auto-generated constructor stub
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.preference.PreferenceActivity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.manufacture_info);
        mNv = new QcNvItems(this);

    }

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onResume()
     */
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        setPreferenceSummary();
    }

    public void setPreferenceSummary() {
        if (mNv == null) {
            mNv = new QcNvItems(this);
        }
        if (mNv != null) {
            mHandler.sendEmptyMessage(1);
        }

    }

    private String getNvData() {

        if (mNv == null) {
            mNv = new QcNvItems(this);
        }
        String NV2499 = "";
        Log.d(TAG, "abcd-------->" + "abcd".substring(1, 2));
        try {
            NV2499 = mNv.getNvFactoryData3I();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

		Log.d(TAG,
                "NV2499--------->" + NV2499.toString() + "\n" + NV2499.length());
        if (NV2499 != null && NV2499.equals("")) {
            Log.d(TAG, "NV2499----0----->"
                    + NV2499.substring(0, 1).replace("B", ""));
            Log.d(TAG, "NV2499----2----->"
                    + NV2499.substring(2, 3).replace("B", ""));
            Log.d(TAG, "NV2499----4----->"
                    + NV2499.substring(3, 4).replace("B", ""));
            Log.d(TAG, "NV2499----5----->"
                    + NV2499.substring(5, 6).replace("B", ""));
            Log.d(TAG, "NV2499----6----->"
                    + NV2499.substring(6, 7).replace("B", ""));
            Log.d(TAG, "NV2499----7----->"
                    + NV2499.substring(7, 8).replace("B", ""));
            Log.d(TAG, "NV2499----8----->"
                    + NV2499.substring(8, 9).replace("B", ""));
            Log.d(TAG, "NV2499----15----->"
                    + NV2499.substring(14, 15).replace("B", ""));

            Log.d(TAG, "NV2499----17----->"
                    + NV2499.substring(16, 17).replace("B", ""));
            Log.d(TAG, "NV2499----18----->"
                    + NV2499.substring(17, 18).replace("B", ""));
            Log.d(TAG, "NV2499----19----->"
                    + NV2499.substring(18, 19).replace("B", ""));
            Log.d(TAG, "NV2499----20----->"
                    + NV2499.substring(19, 20).replace("B", ""));
            Log.d(TAG, "NV2499----21----->"
                    + NV2499.substring(21, 22).replace("B", ""));

            Log.d(TAG, "NV2499----26----->"
                    + NV2499.substring(26, 27).replace("B", ""));
            Log.d(TAG, "NV2499----27----->"
                    + NV2499.substring(27, 28).replace("B", ""));
        }
        if(!(NV2499 == null || NV2499.equals(""))){
        setSummaryText(MACHINE_CHARGE, NV2499.substring(0, 1));
        setSummaryText(COUPLING_TEST, NV2499.substring(2, 3));
        setSummaryText(MMI1_TEST, NV2499.substring(3, 4));
        setSummaryText(MACHINE_CHECK, NV2499.substring(5, 6));
        setSummaryText(FACTORY_DATA_RESET, NV2499.substring(6, 7));
        setSummaryText(SMTSN_NUM, NV2499.substring(7, 8));
        setSummaryText(FTBT_MARK, NV2499.substring(8, 9));
        setSummaryText(MMI2_TEST, NV2499.substring(14, 15));
        setSummaryText(AGEING_TEST, NV2499.substring(16, 17));
        setSummaryText(AUDIO_TEST, NV2499.substring(17, 18));
        setSummaryText(GSM_CALIBRATION, NV2499.substring(18, 19));
        setSummaryText(GSM_TEST, NV2499.substring(19, 20));
        setSummaryText(CDMA_CALIBRATION, NV2499.substring(20, 21));
        setSummaryText(CDMA_TEST, NV2499.substring(21, 22));
        setSummaryText(LTE_CALIBRATION, NV2499.substring(26, 27));
        setSummaryText(LTE_TEST, NV2499.substring(27, 28));
        }

        return NV2499;
    }

    private void setSummaryText(String preferenceKey, String value) {
        Preference preference = findPreference(preferenceKey);
        if (preference == null)
            return;

        if (TextUtils.isEmpty(value)) {
            preference.setSummary(mUnknown);
        } else {
            if ("P".equals(value)) {
                preference.setSummary(getString(R.string.success));
            } else if ("F".equals(value)) {
                preference.setSummary(getString(R.string.fail));
            } else {
                preference.setSummary(mUnknown);
            }
        }
        // preference.setSummary(value);
    }
}
